package com.saltedfish.tool.present

import android.os.Build
import androidx.annotation.RequiresApi
import cn.droidlover.xdroidmvp.bean.BaseModel
import cn.droidlover.xdroidmvp.log.XLog
import cn.droidlover.xdroidmvp.mvp.XPresent
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.NetError
import cn.droidlover.xdroidmvp.net.XApi
import com.google.gson.Gson
import com.saltedfish.tool.model.LoginInfo
import com.saltedfish.tool.model.ModelInfo
import com.saltedfish.tool.model.ThirdLoginInfo
import com.saltedfish.tool.model.User
import com.saltedfish.tool.net.Api
import com.saltedfish.tool.ui.user.LoginActivity
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


class TextAP:XPresent<LoginActivity>(){

    /**
     * 登录模块
     * @param
     */
    fun login(login: ThirdLoginInfo) {

        val gson = Gson()
        val postInfoStr = gson.toJson(login)
        val body: RequestBody = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            postInfoStr
        )
        Api.getDyService().login(body)
            .compose(XApi.getApiTransformer<BaseModel<LoginInfo>>())
            .compose(XApi.getScheduler<BaseModel<LoginInfo>>(v))
            .subscribe(object : ApiSubscriber<BaseModel<LoginInfo>>() {
                override fun onFail(error: NetError) {
                    XLog.e("wx Info","-----$error")

                }

                override fun onNext(result: BaseModel<LoginInfo>) {
                    XLog.e("wx Info", "-wx Info-111---$result")
                    if(result.code == 200){
                        User.getInstance().loginSuccess(result.result)
                        v.loginSuccess(result.result.userInfo)
                    }

                }
            })



    }


    /**
     * 上传文件
     *
     * @param filePath
     */
    @RequiresApi(Build.VERSION_CODES.M)
    fun uploadFile(filePath: String) {
        val file = File(filePath)
        val body = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val multipartBody = MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.name, body)
                .build()
        Api.getDyService().uploadFile(multipartBody.part(0))
                .compose(XApi.getScheduler<BaseModel<ModelInfo>>(v))
                .compose(XApi.getApiTransformer<BaseModel<ModelInfo>>())
                .compose(v.bindToLifecycle())
                .subscribe(object : ApiSubscriber<BaseModel<ModelInfo>>() {
                    override fun onFail(error: NetError) {
                        XLog.e("wx Info","-----$error")

                    }

                    override fun onNext(result: BaseModel<ModelInfo>) {
                        XLog.e("wx Info", "-----"+result.toString())
//                        if(result.code == 200){
//                            User.getInstance().loginSuccess(result.result.userInfo)
//                            v.loginSuccess(result.result.userInfo)
//                        }

                    }

                })


    }



    /**
     * 下載
     * @param
     */
    fun downloadFile() {

//        Api.getDyService().downloadFile()
//                .compose(XApi.getApiTransformer<BaseModel>())
//                .compose(XApi.getScheduler<BaseModel<ModelInfo>>(v))
//                .subscribe(object : ApiSubscriber<BaseModel<ModelInfo>>() {
//                    override fun onFail(error: NetError) {
//                        XLog.e("wx Info","-----$error")
//
//                    }
//
//                    override fun onNext(result: BaseModel<ModelInfo>) {
//                        XLog.e("wx Info", "-----"+result.result.toString())
////                        if(result.code == 200){
////                            User.getInstance().loginSuccess(result.result.userInfo)
////                            v.loginSuccess(result.result.userInfo)
////                        }
//
//                    }
//                })

    }



}